package com.cloudbroker.bcs.common.jres.service;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

import com.cloudbroker.bcs.common.jres.util.JresUtil;

public abstract class APIServiceT2AdapterBase<T> extends FunctionIdAccessServiceAdapterBase<T> {
    
    @SuppressWarnings("unchecked")
    protected <B> Map<String, Object> callT2(String functionId, B param) {
        // 获取方法
        Method method = getMethod(functionId);
        try {
            // 调用接口方法
            Map<String, Object> jresResultMap = (Map<String, Object>) method.invoke(getTargetService(), param);
            return jresResultMap;
        } catch (Exception e) {
            throw new RuntimeException("call t2 method error", e);
        }
    }


    protected <B,V> Map<String, V> callT2ByConvert(String functionId, B param) throws  ClassCastException{
        Map<String,V> newMap = new LinkedHashMap<>();
        Map<String,Object> resMap =  callT2(functionId,param);
        for (String key : resMap.keySet()) {
            newMap.put(key,(V)resMap.get(key));
        }
        return newMap;
    }
}
